<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace Sun\Html;

class PageSwitch extends Component {

    private $div;
    private $content;
    private $panel;
    public function __construct($pref, $visible_pages = 7) {
        $this->div = new Div(array(
            'id' => $pref."PageSwitch",
            'class' => "PageSwitch"));
        $this->content = $this->div->Add(new Div(array(
            'id' => $pref."PageContent",
            'class' => "Content"
        )), "content")->Added();
        $src = <<<SCRIPT

jsun.run(function() {
////////////////////////////////////////////
    var pref = "{$pref}";
    jsun.swap[pref] = {};

    function CreatePanel(num_page) {

        if (num_page > 1) {
            var num_vis = {$visible_pages},
                P = parseInt(jsun.swap[pref]["Page"]),
                media = Math.floor(num_vis / 2),
                SP, FP;

            if (num_vis >= num_page) {
                SP = 1;
                FP = num_page;
            } else if (P - media <= 1) {
                SP = 1;
                FP = num_vis;
            } else if (P + media >= num_page) {
                FP = num_page;
                SP = FP - num_vis + 1;
            } else {
                SP = P - media;
                FP = SP + num_vis - 1;
            }
            var panel, cases = [];
            for (var i = SP; i <= FP; ++i) {
                if (i == P) {
                    cases.push({tag: "span", body: [{text: i}],
                        attr: {id: pref + "SelectPage", "class": "SelectPage", "data-p": i}
                    });
                } else {
                    cases.push({tag: "a",
                        body: [{text: (((i == SP && i > 1) || (i == FP && i < num_page)) ? "..." : i)}],
                        attr: {
                            id: pref + "P" + i, "class": "Link",
                            "data-p": i, href: i
                        }
                    });
                }
            }
        }
        panel = jsun.dom({ tag: "div", body: cases,
            attr: {id: pref + "PagePanel", "class": "SwitchPanel"}
        });
        for (i in panel.childNodes) {
            var e = panel.childNodes[i];
            if (e.nodeType == document.ELEMENT_NODE && e.nodeName == "A")
                e.jsun.event({ onclick: GetPage });
        }
        var old_panel = jsun("#" + pref + "PagePanel"),
            page_switch = jsun("#" + pref + "PageSwitch");
        if (old_panel) page_switch.replaceChild(panel, old_panel);
        else page_switch.appendChild(panel);
    }

    function GetPage() {
        var p = this.getAttribute("data-p"),
            param = jsun.swap[pref].Req + "&Page=" +p,
            content = jsun("#" + pref + "PageContent");

        jsun.json.load(function(p) {
            try {
                if (p.content) {
                    content.innerHTML = "";
                    content.appendChild(jsun.dom.from_json(p.content));
                    CreatePanel(p.num_pages);
                }
            } catch(e) {
                //jsun.dialog.warning('Ошибка', e).show();
            }
        }, param);

        jsun.swap[pref].Page = p;
        return true;
    }

    jsun.swap[pref].Start = function() {
        ({ GetPage: GetPage,
            getAttribute: function() { return jsun.swap[pref].Page ? jsun.swap[pref].Page : 1; }
        }).GetPage();
        return true;
    };
///////////////////////////////////////
});

SCRIPT;
        $this->script = new Script($src, array(
            'id' => $pref."PageSwitchScript"));
        $this->div->Add($this->script, "script");
    }

    public function HtmlContent() { }
    public function JSONContent() { }

    public function ToHtml() {
        return $this->div->ToHtml();
    }
    public function ToJSON() {
        return $this->div->ToJSON();
    }
    public function Div() {
        return $this->div;
    }
    public function PageContent() {
        return $this->content;
    }
    public function PagePanel() {
        return $this->panel;
    }
}
